import { ConfigureStoreOptions } from "@reduxjs/toolkit";

export type ComponentType = React.ComponentType<unknown> | React.FunctionComponent<unknown>;


export interface IComponentType {
    component: ComponentType;
    props?: Record<string, unknown>; 
}

export interface IRegisterProps {
    component: ComponentType | Array<IComponentType>;
    props?: Record<string, unknown>;
    slot: string;
    componentSlot?: string[]
}

export interface ISlot {
    [key: string]: Array<IRegisterProps>;
}

// export type IRegisterSlotProps = IRegisterProps;

export interface ISlotRegister {
    slots: ISlot
    // registerSlot(props: IRegisterSlotProps): void;
    register(props: IRegisterProps) : void
}


export class SlotRegister implements ISlotRegister{

    slots: ISlot = {
        layout: []
    };

    register(props: IRegisterProps) {
        const isExistSlot = Array.isArray(this.slots[props.slot]);
        if (!isExistSlot) {
            console.warn('【register:】' + props.slot + ' is not exist, please ensure that the slot container exists')
        } else {
            this.slots[props.slot].push(props)
            if(props.componentSlot && Array.isArray(props.componentSlot)) {
                props.componentSlot.map(slot => {
                    const isExistSlot = Array.isArray(this.slots[slot]);
                    if (isExistSlot) {
                        console.warn(`【register:】在${props.slot}中添加子插槽容器失败，${slot}插槽容器已存在`)
                    } else {
                        this.slots[slot] = [];
                    }
                })
            }
        }
    }
}  



export interface IReducerRegister {
    reducer: ConfigureStoreOptions['reducer'];
    register(key: string, reducer: ConfigureStoreOptions['reducer']): void;
}

export class ReducerRegister {
    reducer: ConfigureStoreOptions['reducer'] = {};

    constructor() {
        this.reducer = {};
    }

    register(key: string, reducer: ConfigureStoreOptions['reducer']) {
        this.reducer = Object.assign(this.reducer, { [key]: reducer })
    }
}

/**
 * 模块注册器
 * 支持插槽中注册组件，注册reducer
 */
export interface IModuleRegistry {
    /**
     * 往插槽中注册组件
     * @param register 
     * @returns 
     */
    registerComponent?: (register: ISlotRegister) => void;
    /**
     * 全局注册reducer
     * @param register 
     * @returns 
     */
    registerReducer?: (register: IReducerRegister) => void;
}


